package com.example.mytest6.di_er_bian;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.example.mytest6.R;
import java.util.ArrayList;
import java.util.List;


public class Chart_ana_Act extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "Chart_ana_Act";
    private ViewPager viewPager;
    private RadioButton rb_1;
    private RadioButton rb_2;
    private RadioButton rb_3;
    private RadioButton rb_4;
    private RadioButton rb_5;
    private RadioButton rb_6;
    private RadioButton rb_7;
    List<RadioButton> rbList = new ArrayList<>();
    List<Fragment> fgList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chart_ana);
        initFgList();
        initView();
        initData();
    }

    private void initFgList() {
        fgList.add(new Pie_1());
        fgList.add(new Pie_1());
        fgList.add(new Pie_1());
        fgList.add(new Pie_1());
        fgList.add(new Pie_1());
        fgList.add(new Pie_1());
        fgList.add(new Pie_1());
        rbList.add((RadioButton) findViewById(R.id.rb_1));
        rbList.add((RadioButton) findViewById(R.id.rb_2));
        rbList.add((RadioButton) findViewById(R.id.rb_3));
        rbList.add((RadioButton) findViewById(R.id.rb_4));
        rbList.add((RadioButton) findViewById(R.id.rb_5));
        rbList.add((RadioButton) findViewById(R.id.rb_6));
        rbList.add((RadioButton) findViewById(R.id.rb_7));
    }

    private void initData() {
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fgList);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        change(0);
                        break;
                    case 1:
                        change(1);
                        break;
                    case 2:
                        change(2);
                        break;
                    case 3:
                        change(3);
                        break;
                    case 4:
                        change(4);
                        break;
                    case 5:
                        change(5);
                        break;
                    case 6:
                        change(6);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    private void change(int i) {
        for (int j = 0; j < rbList.size(); j++) {
            if (j == i) {
                rbList.get(j).setButtonDrawable(R.drawable.gray_bg);
                rbList.get(j).setChecked(true);
            } else {
                rbList.get(j).setButtonDrawable(R.drawable.white_bg);
            }
        }
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.view);
        rb_1 = (RadioButton) findViewById(R.id.rb_1);
        rb_2 = (RadioButton) findViewById(R.id.rb_2);
        rb_3 = (RadioButton) findViewById(R.id.rb_3);
        rb_4 = (RadioButton) findViewById(R.id.rb_4);
        rb_5 = (RadioButton) findViewById(R.id.rb_5);
        rb_6 = (RadioButton) findViewById(R.id.rb_6);
        rb_7 = (RadioButton) findViewById(R.id.rb_7);
        rb_1.setOnClickListener(this);
        rb_2.setOnClickListener(this);
        rb_3.setOnClickListener(this);
        rb_4.setOnClickListener(this);
        rb_5.setOnClickListener(this);
        rb_6.setOnClickListener(this);
        rb_7.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_1:
                viewPager.setCurrentItem(0);
                change(0);
                break;
            case R.id.rb_2:
                viewPager.setCurrentItem(1);
                change(1);
                break;
            case R.id.rb_3:
                viewPager.setCurrentItem(2);
                change(2);
                break;
            case R.id.rb_4:
                viewPager.setCurrentItem(3);
                change(3);
                break;
            case R.id.rb_5:
                viewPager.setCurrentItem(4);
                change(4);
                break;
            case R.id.rb_6:
                viewPager.setCurrentItem(5);
                change(5);
                break;
            case R.id.rb_7:
                viewPager.setCurrentItem(6);
                change(6);
                break;
        }

    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        List<Fragment> fglist;

        public ViewPagerAdapter(@NonNull FragmentManager fm, List<Fragment> fglist) {
            super(fm);
            this.fglist = fglist;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fglist.get(position);
        }

        @Override
        public int getCount() {
            return fgList.size();
        }
    }
}
